<?php

if (!function_exists('get_millisecond')) {
    /**
     * Gets the number of milliseconds of the current time
     *
     * @return float
     */
    function get_millisecond()
    {
        list($t1, $t2) = explode(' ', microtime());

        return (int)sprintf('%.0f', (floatval($t1) + floatval($t2)) * 1000);
    }
}

if (!function_exists('get_url_query')) {
    /**
     * 将参数变为字符串
     *
     * @param $array_query
     *
     * @return string string 'm=content&c=index&a=lists&catid=6&area=0&author=0&h=0®ion=0&s=1&page=1' (length=73)
     */
    function get_url_query($array_query)
    {
        $tmp = array();
        foreach ($array_query as $k => $param) {
            $tmp[] = $k . '=' . $param;
        }
        $params = implode('&', $tmp);

        return $params;
    }
}
    if (!function_exists('parseEntityFieldParams')) {
        function parseEntityFieldParams($params)
        {
            if (strpos($params, 'getFormItemsFrom') === 0 && function_exists($params)) {
                $params = call_user_func($params);
            }
            $items = explode("\n", $params);
            return array_map(function ($item) {
                return explode("=", $item);
            }, $items);
        }
    }

    if (!function_exists('parseEntityFieldParamsForXmSelect')){
        function parseEntityFieldParamsForXmSelect($params) {
            $items = explode("\n", $params);
            $select_params = array_map(function ($item) {
                return explode("=", $item);
            }, $items);
            $arr = [];
            foreach ($select_params as $key=>$param) {
                $arr[$key]['name']= $param[0];
                $arr[$key]['value']= $param[1];
            }
            return json_encode($arr,JSON_UNESCAPED_UNICODE);
        }
    }


if (!function_exists('disk_path')) {
    /**
     * @param          $path
     * @param string $diskNo
     *
     * @return array
     * @throws Exception
     */
    function disk_path($path, $diskNo = 'disk_0')
    {
        $diskPaths = [
            'disk_0' => 'uploads/'
        ];
        if (!isset($diskPaths[$diskNo])) {
            throw new Exception($diskNo . ' 磁盘设置不存在');
        }
        $diskPrefix = $diskPaths[$diskNo];

        return [
            'db_path' => $diskPrefix . $path,
        ];
    }
}

if (!function_exists('split_url')) {
    function split_url($url)
    {
        return explode(':', $url);
    }
}

if (!function_exists('selected_or_not')) {
    function selected_or_not($from, $to)
    {
        if ($from === $to) {
            return 'selected';
        }

        return '';
    }
}

if (!function_exists('get_md5_random_str')) {
    /**
     * Gets a random MD5 string
     *
     * @return string
     */
    function get_md5_random_str()
    {
        return md5(time() . uniqid() . str_random(16));
    }
}

if (!function_exists('array_key_increment')) {
    function array_key_increment($array) {
        foreach ($array as $k => $v) {
            $k++;
            $new_array[$k] = $v;
        }
        return $new_array;
    }
}
if (!function_exists('isChecked')) {
    function isChecked($value, $options)
    {
        return in_array($value, explode(',', $options), true);
    }
}

if (!function_exists('create_js')) {
    function create_js($carouse,$file_name) {
        $id = $carouse->ID;
        $img_url = $carouse->img_url;
        $url = $carouse->url;
        $title = $carouse->title;
        $paddingLeft = random_int(500,1900);
        $paddingTop = random_int(130,970);
        $js_str= <<<php
function PCMSAD(PID) {
  this.ID        = PID;
  this.PosID  = 0; 
  this.ADID		  = 0;
  this.ADType	  = "";
  this.ADName	  = "";
  this.ADContent = "";
  this.PaddingLeft = 0;
  this.PaddingTop  = 0;
  this.Width = 0;
  this.Height = 0;
  this.IsHitCount = "N";
  this.UploadFilePath = "";
  
  this.Step = 1;
  this.Delay= 20;
  this.WindowHeight = 0;
  this.WindowWidth = 0;
  this.Yon = 0;
  this.Xon = 0;
  this.Pause = true;
  this.Interval = null;
  this.URL = "";
  this.SiteID = 0;
  
  this.ShowAD  = showADContent;
  this.Start   = doStart;
  this.Stat = statAD;
}

function statAD() {
	var new_element = document.createElement("script");
	new_element.type = "text/javascript";
	//new_element.src="http://www.hbkx.gov.cn/index.php?m=poster&c=index&a=show&siteid="+this.SiteID+"&spaceid="+this.ADID+"&id="+this.PosID;
	//console.log(new_element.src)
	document.body.appendChild(new_element);
}

function showADContent() {
  var content = this.ADContent;
  var str = "<div id='PCMSAD_"+this.PosID+"' style='left:"+this.PaddingLeft+"px;top:"+this.PaddingTop+"px;width:"+this.Width+"px; height:"+this.Height+"px; position: absolute;visibility: visible;z-index:999999;' onMouseOver='"+this.ID+"_pause_resume();' onMouseOut='"+this.ID+"_pause_resume();'>";
  var AD = eval('('+content+')');
  if (this.ADType == "images") {
	  str += "<a href='"+AD.Images[0].imgADLinkUrl+"' target='_blank'>";
	  str += "<img title='"+AD.Images[0].imgADAlt+"' src='"+this.UploadFilePath+AD.Images[0].ImgPath+"' width='"+this.Width+"' height='"+this.Height+"' style='border:0px;'>";
	  str += "</a>";
  }
  str += "<div style='text-align:right;'><a href='#;' onclick='javascript:document.getElementById(\"PCMSAD_"+this.PosID+"\").style.display=\"none\"'>关闭</a></div>";
  str += "</div>";
  document.write(str);
}

function changePos(float) {	
	float.WindowWidth  = document.compatMode == "BackCompat" ? document.body.clientWidth : document.documentElement.clientWidth;
	float.WindowHeight = document.compatMode == "BackCompat" ? document.body.clientHeight : document.documentElement.clientHeight;
	document.getElementById("PCMSAD_"+float.PosID).style.left = (float.PaddingLeft + (Math.max(document.documentElement.scrollLeft, document.body.scrollLeft)))+"px";
	document.getElementById("PCMSAD_"+float.PosID).style.top = (float.PaddingTop + (Math.max(document.documentElement.scrollTop, document.body.scrollTop)))+"px";
	if (float.Yon){
		float.PaddingTop = float.PaddingTop + float.Step;
	}else{
		float.PaddingTop = float.PaddingTop - float.Step;
	}
	if (float.PaddingTop < 0){
		float.Yon = 1;
		float.PaddingTop = 0;
	}
	if (float.PaddingTop >= (float.WindowHeight - float.Height)){
		float.Yon = 0;float.PaddingTop = (float.WindowHeight - float.Height);
	}
	if (float.Xon){
		float.PaddingLeft = float.PaddingLeft + float.Step;
	}else{
		float.PaddingLeft = float.PaddingLeft - float.Step;
	}
	if (float.PaddingLeft < 0){
		float.Xon = 1;
		float.PaddingLeft = 0;
	}
	if (float.PaddingLeft >= (float.WindowWidth - float.Width)){
		float.Xon = 0;
		float.PaddingLeft = (float.WindowWidth - float.Width);   
	}
}
	
function doStart(float){
	return function(){
        changePos(float);
    }
}
 
function cmsAD_{$id}_pause_resume(){if(cmsAD_{$id}.Pause){clearInterval(cmsAD_{$id}.Interval);cmsAD_{$id}.Pause = false;}else {cmsAD_{$id}.Interval = setInterval(cmsAD_{$id}.Start(cmsAD_{$id}),cmsAD_{$id}.Delay);cmsAD_{$id}.Pause = true;}} 
var cmsAD_{$id} = new PCMSAD('cmsAD_{$id}'); 
cmsAD_{$id}.PosID = {$id}; 
cmsAD_{$id}.ADID = {$id}; 
cmsAD_{$id}.ADType = "images"; 
cmsAD_{$id}.ADName = "{$title}"; 
cmsAD_{$id}.ADContent = "{'Images':[{'imgADLinkUrl':'{$url}','imgADAlt':'','ImgPath':'{$img_url}'}],'imgADLinkTarget':'New','Count':'1','showAlt':'Y'}";
cmsAD_{$id}.SiteID = 1; 
cmsAD_{$id}.PaddingLeft = {$paddingLeft}; 
cmsAD_{$id}.PaddingTop = {$paddingTop}; 
cmsAD_{$id}.Width = 250; 
cmsAD_{$id}.Height = 120;
cmsAD_{$id}.UploadFilePath = "";
cmsAD_{$id}.ShowAD();

var isIE=!!window.ActiveXObject; 
if (isIE){

	if (document.readyState=="complete"){
		cmsAD_{$id}.Stat();
	} else {
		document.onreadystatechange=function(){
			if(document.readyState=="complete") cmsAD_{$id}.Stat();
		}
	}
} else {
	cmsAD_{$id}.Stat();
}
document.getElementById('PCMSAD_{$id}').visibility = 'visible';
cmsAD_{$id}.Interval = setInterval(cmsAD_{$id}.Start(cmsAD_{$id}),cmsAD_{$id}.Delay);

php;
        file_put_contents($file_name,$js_str,LOCK_EX);

    }
}
